/* eslint-disable no-async-promise-executor */
import { loginApi, getInfoApi, getUserDetailById } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userInfo: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
    setToken(token)
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_INFO: (state, userInfo) => {
    state.userInfo = userInfo
  }
}

const actions = {
  // user login
  login(context, userInfo) {
    // console.log(userInfo)
    // const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      loginApi(userInfo)
        .then((response) => {
          // console.log(response)
          const { data } = response

          context.commit('SET_TOKEN', data)
          // setToken(data.token)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise(async(resolve, reject) => {
      try {
        const response = await getInfoApi()

        const res = await getUserDetailById(response.data.userId)
        const { data } = response
        commit('SET_NAME', data.username)
        commit('SET_INFO', data)
        commit('SET_AVATAR', res.data.staffPhoto)
        resolve(data)
      } catch (err) {
        reject(err)
      }
      // getInfoApi()
      //   .then((response) => {
      //     // console.log(response.data.userId, 111)
      //     getUserDetailById(response.data.userId).then(res => {
      //       console.log(res, 777)
      //       commit('SET_AVATAR', res.data.staffPhoto)
      //     })
      //     // console.log(response, 666)
      //     const { data } = response
      //     // 1.permisssion.js 触发getInfo函数帮助获取用户信息
      //     // 2.store/modile/user.js 发送请求获取用户信息
      //     // 3.将用户信息存储到state中
      //     //   3.1.state中顶一个userInfo对象
      //     //   3.2.在mutation中定义一个函数来更改userInfo=SET_INFO数据的函数
      //     //   3.3.调用SET_INFO来更改数据
      //     // console.log(data)
      //     commit('SET_NAME', data.username)
      //     commit('SET_INFO', data)
      //     resolve(data)
      //   })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      resetRouter()
      commit('RESET_STATE')
      resolve().catch((error) => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
